package net.srt.quartz.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.srt.api.module.data.DataAccessApi;
import net.srt.api.module.data.constant.CommonRunStatus;
import net.srt.api.module.data.constant.TaskType;
import net.srt.api.module.data.dto.DataAccessDto;
import net.srt.api.module.data.dto.DataAccessTaskDto;
import net.srt.framework.security.user.SecurityUser;
import net.srt.quartz.utils.CronUtils;
import org.springframework.stereotype.Component;
import srt.cloud.framework.dbswitch.data.config.DbswichProperties;
import srt.cloud.framework.dbswitch.data.domain.DbSwitchResult;
import srt.cloud.framework.dbswitch.data.service.MigrationService;
import srt.cloud.framework.dbswitch.data.util.BytesUnitUtils;

import java.util.Date;

/**
 * @ClassName DataAccessTask
 * @Author zrx
 * @Date 2022/10/26 13:12
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DataAccessTask {

	private final DataAccessApi dataAccessApi;

	public void syncData(String dataAccessId) {
		DataAccessDto dataAccessDto = dataAccessApi.getById(Long.parseLong(dataAccessId)).getData();
		DbswichProperties dbswichProperties = dataAccessDto.getDataAccessJson();
		MigrationService service = new MigrationService(dbswichProperties);
		DataAccessTaskDto dataAccessTaskDto = new DataAccessTaskDto();
		try {
			dataAccessTaskDto.setDataAccessId(Long.parseLong(dataAccessId));
			dataAccessTaskDto.setRunStatus(CommonRunStatus.RUNNING.getCode());
			dataAccessTaskDto.setStartTime(new Date());
			dataAccessTaskDto.setId(dataAccessApi.addTask(dataAccessTaskDto).getData());
			//执行任务
			DbSwitchResult dbSwitchResult = service.run();
			//添加同步结果
			dataAccessApi.addTaskDetail(dataAccessTaskDto.getId(), dataAccessTaskDto.getDataAccessId(), dbSwitchResult);
			dataAccessTaskDto.setEndTime(new Date());
			dataAccessTaskDto.setDataCount(dbSwitchResult.getTotalRowCount().get());
			dataAccessTaskDto.setTableSuccessCount(dbSwitchResult.getTotalTableCount().get());
			dataAccessTaskDto.setTableFailCount(dbSwitchResult.getTotalTableFailCount().get());
			dataAccessTaskDto.setByteCount(BytesUnitUtils.bytesSizeToHuman(dbSwitchResult.getTotalBytes().get()));
			dataAccessTaskDto.setRunStatus(dbSwitchResult.getIfAllSuccess().get() ? CommonRunStatus.SUCCESS.getCode() : CommonRunStatus.FAILED.getCode());
			dataAccessTaskDto.setErrorInfo(dbSwitchResult.getIfAllSuccess().get() ? null : String.format("有%s张表同步失败，可以在同步结果查看！", dataAccessTaskDto.getTableFailCount()));
			dataAccessTaskDto.setNextRunTime(TaskType.ONE_TIME_FULL_PERIODIC_INCR_SYNC.getCode().equals(dataAccessDto.getTaskType()) ? CronUtils.getNextExecution(dataAccessDto.getCron()) : null);
			dataAccessApi.updateTask(dataAccessTaskDto);
		} catch (Exception e) {
			log.error("syncTask error：" + e.getMessage(), e);
			dataAccessTaskDto.setEndTime(new Date());
			dataAccessTaskDto.setRunStatus(CommonRunStatus.FAILED.getCode());
			dataAccessTaskDto.setNextRunTime(TaskType.ONE_TIME_FULL_PERIODIC_INCR_SYNC.getCode().equals(dataAccessDto.getTaskType()) ? CronUtils.getNextExecution(dataAccessDto.getCron()) : null);
			dataAccessTaskDto.setErrorInfo(e.getMessage());
			dataAccessApi.updateTask(dataAccessTaskDto);
		}
	}
}


